#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <cstdio>

using namespace std;

typedef struct THIGHWAY {
	int from, to;
	string fromCity, toCity;
	string name;
	int miles;
} THIGHWAY;

istream& operator >> (istream& in, THIGHWAY& h) {
	char line[100];
	memset(line, 0, 100);
	in.getline(line, 100);
	char ch;
	if (sscanf(line, " %c", &ch) == -1) {
		h.from = -1;
		return in;
	}
	char *ptr;
	ptr = strtok(line, ",");
	h.fromCity = string(ptr);
	ptr = strtok(NULL, ",");
	h.toCity = string(ptr);
	ptr = strtok(NULL, ",");
	h.name = string(ptr);
	ptr = strtok(NULL, "\n");
	h.miles = atoi(ptr);
	return in;
}

vector<THIGHWAY> highways;
vector<string> cities;

int n, m;
int map[110][110];
int path[110][110];

void buildmap(void) {
	m = highways.size();
	for (int i=0; i<m; ++i) {
		cities.push_back(highways[i].fromCity);
		cities.push_back(highways[i].toCity);
	}
	sort(cities.begin(), cities.end());
	cities.resize(unique(cities.begin(),cities.end())-cities.begin());
	n = cities.size();
	for (int i=0; i<m; ++i) {
		for (int j=0; j<n; ++j) {
			if (highways[i].fromCity == cities[j])
				highways[i].from = j;
			if (highways[i].toCity == cities[j])
				highways[i].to = j;
		}
	}

// 	for (int i=0; i<n; ++i)
// 		cout << i<< " " << cities[i] << endl;
// 	for (int i=0; i<m; ++i)
// 		cout<< highways[i].from << " " << highways[i].to << " "
// 			<< highways[i].fromCity << " " << highways[i].toCity << " "
// 			<< highways[i].name << " " << highways[i].miles << " "
// 			<< endl;


	for (int i=0; i<n; ++i) {
		for (int j=0; j<n; ++j)
			map[i][j] = 1050000000;
		map[i][i] = 0;
	}

	for (int i=0; i<n; ++i) {
		for (int j=0; j<n; ++j)
			path[i][j] = -1;
	}


	for (int i=0; i<m; ++i) {
		int a, b;
		a = highways[i].from;
		b = highways[i].to;
		if (highways[i].miles < map[a][b])
			map[a][b] = map[b][a] = highways[i].miles;
	}

}

void dijkstra(void) {
	for (int k=0; k<n; ++k) {
		for (int i=0; i<n; ++i) {
			for (int j=i+1; j<n; ++j) {
				if (map[i][k]+map[k][j]<map[i][j]) {
					map[j][i] = map[i][j] = map[i][k]+map[k][j];
					path[i][j] = path[j][i] = k;
				}
			}
		}
	}
}

void showTBody(int from, int to) {
	if (path[from][to] != -1) {
		showTBody(from, path[from][to]);
		showTBody(path[from][to], to);
	}
	else {
		THIGHWAY h;
		h.from = -1;
		for (int i=0; i<m; ++i)
			if ((highways[i].from == from && highways[i].to == to && highways[i].miles == map[from][to]) ||
				(highways[i].to == from && highways[i].from == to && highways[i].miles == map[from][to])){
				h = highways[i];
				break;
			}
	
		cout << setw(20) << left << cities[from] << " "
			<< setw(20) << left << cities[to] << " "
			<< setw(10) << left << h.name << " "
			<< setw(5) << right << h.miles << endl;
	}
}

void showTable(int from, int to) {
	cout<< endl << endl
		<< "From                 To                   Route      Miles" << endl
		<< "-------------------- -------------------- ---------- -----" << endl;
	showTBody(from, to);
	cout<< "                                                     -----" << endl
		<< "                                          Total      " 
		<< setw(5) << right << map[from][to] << endl;
}

int main(void) {

	while (true) {
		THIGHWAY hway;
		cin >> hway;
		if (hway.from == -1) break;
		highways.push_back(hway);
	}

	buildmap();

	dijkstra();

// 	for (int i=0; i<n; ++i) {
// 		for (int j=0; j<n; ++j)
// 			cout << setw(2) << path[i][j] << ' ';
// 		cout << endl;
// 	}

	char line[100];
	while (true) {
		memset(line, 0, 100);
		cin.getline(line, 100);

		char ch;
		if (sscanf(line, " %c", &ch) == -1) break;

		string city;
		char *cityPtr;
		int from, to;
		cityPtr = strtok(line, ",");
		city = string(cityPtr);
		for (int i=0; i<n; ++i) {
			if (city == cities[i]) {
				from = i;
				break;
			}
		}
		cityPtr = strtok(NULL, "\n");
		city = string(cityPtr);
		for (int i=0; i<n; ++i) {
			if (city == cities[i]) {
				to = i;
				break;
			}
		}

		showTable(from, to);
	}

	return 0;
}
